//
//  ZQMoneySettingPage.m
//  jianzhik
//
//  Created by huami on 2018/8/28.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "UIView+zq_extension.h"
#import "ZQMoneySettingPage.h"
#import "ZQDisplayButtonView.h"
#import "ZQListTableViewCell.h"

#define kMax  3

@interface ZQMoneySettingPage ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *yesOrNoView;
@property (weak, nonatomic) IBOutlet UITextField *moneyNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *kgfFreeTextField;

/** ivButton */
@property (nonatomic, weak) UIButton *ivButtoon;
@property (nonatomic, strong) NSMutableArray * boolArr;
/** headViewButton */
@property (nonatomic, weak) ZQDisplayButtonView *headViewButton;
/** 记录菜单文字 */
@property (nonatomic, copy) NSString *menuStr;
@end

@implementation ZQMoneySettingPage

- (NSString *)menuStr
{
    if (!_menuStr) {
        _menuStr = @"是";
    }
    return _menuStr;
}


- (ZQDisplayButtonView *)headViewButton
{
    if (!_headViewButton) {
        
        ZQDisplayButtonView *headViewButton = [ZQDisplayButtonView ZQ_ViewFromXib];
        _headViewButton = headViewButton;
    }
    return _headViewButton;
}

- (NSMutableArray *)boolArr {
    
    if (!_boolArr) {
        
        _boolArr = [NSMutableArray arrayWithObjects:@"NO", nil];
    }
    return _boolArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.footButton setTitle:@"确认添加" forState:UIControlStateNormal];
    [self.footButton addTarget:self action:@selector(addMoney) forControlEvents:UIControlEventTouchUpInside];
    [self registerCell:[ZQListTableViewCell class]];
    self.moneyNameTextField.delegate = self;
}

- (void)addMoney
{
    if (self.moneyNameTextField.text.length == 0 || self.kgfFreeTextField.text == 0) {
        [UIHelper showTipsWithText:@"币种名称或矿工费不能为空" inView:self.view];
        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"Symbol"] = self.moneyNameTextField.text;
    parameters[@"Fee"] = self.kgfFreeTextField.text;
    parameters[@"Contract"] = self.addressTextField.text;
    parameters[@"Type"] = [self.menuStr isEqualToString:@"是"] ? @10 : @20;

    [ZQAFHelper afnetworkRequestWithPostURL:@"/v1/user/addcoin" parameters:parameters view:self.view finishedBlcok:^(NSDictionary *dict) {
        [UIHelper showSuccessMessage:dict[@"Msg"] afterHideHud:2 toView:self.view completion:^{
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.yesOrNoView.frame;
//    self.ivButtoon.frame = self.yesOrNoView.frame;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return self.yesOrNoView.height / 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return self.yesOrNoView.height / 2;
}

#pragma mark - tableView delegate and dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if ([self.boolArr[0] boolValue] == NO) {

        return 0;
    }else {

        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    ZQListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID  forIndexPath:indexPath];
    NSString *str = [self.menuStr isEqualToString:@"是"] ? @"否":@"是";
    //    cell.textLabel.text = str;
    [cell.listButton setTitle:str forState:UIControlStateNormal];

    NSLog(@"cell.listButton%@",[cell.listButton currentTitle]);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.menuStr = [self.menuStr isEqualToString:@"是"] ? @"否":@"是";

    [self.boolArr replaceObjectAtIndex:0 withObject:@NO];

    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    //创建header的view


    UIImage *currentImg = [_boolArr[section] boolValue] ? [UIImage imageNamed:@"buddy_header_arrow_down"] : [UIImage imageNamed:@"buddy_header_arrow_right"];
    //    [ivButton sizeToFit];
    [self.headViewButton.headViewButton setImage:currentImg forState:UIControlStateNormal];
    [self.headViewButton.headViewButton setTitleColor:[UIColor stringToColor:@"#1890ff"] forState:UIControlStateNormal];
    [self.headViewButton.headViewButton setTitle:self.menuStr forState:UIControlStateNormal];



    //添加轻扣手势
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
    [self.headViewButton.headViewButton addGestureRecognizer:tap];

    return self.headViewButton;
}


#pragma mark - action
- (void)tapGR:(UITapGestureRecognizer *)tapGR {

    //判断改变bool值
    if ([_boolArr[0] boolValue] == YES) {
        [_boolArr replaceObjectAtIndex:0 withObject:@NO];
    }else {
        [_boolArr replaceObjectAtIndex:0 withObject:@YES];
    }
    //刷新某个section
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//    if (textField == self.moneyNameTextField) {
//        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
//        if (range.length == 1 && string.length == 0) {
//            return YES;
//        }
//        //so easy
//        else if (self.moneyNameTextField.text.length >= 3) {
//            self.moneyNameTextField.text = [textField.text substringToIndex:3];
//            return NO;
//        }
//    }
//    return YES;
//}

@end
